package org.jeecg.modules.reporting.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.engine.entity.Frame;
import org.jeecg.modules.engine.entity.Task;
import org.jeecg.modules.engine.mapper.FrameMapper;
import org.jeecg.modules.engine.mapper.TaskMapper;
import org.jeecg.modules.reporting.entity.MyReportingSearchVo;
import org.jeecg.modules.reporting.entity.MyReportingVo;
import org.jeecg.modules.reporting.entity.Reporting;
import org.jeecg.modules.reporting.entity.ReportingVo;
import org.jeecg.modules.reporting.mapper.ReportingMapper;
import org.jeecg.modules.reporting.service.IReportingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Description: 报工表
 * @Author: jeecg-boot
 * @Date:   2020-07-23
 * @Version: V1.0
 */
@Service
public class ReportingServiceImpl extends ServiceImpl<ReportingMapper, Reporting> implements IReportingService {

    @Autowired
    private TaskMapper taskMapper;
    @Autowired
    private FrameMapper frameMapper;

    @Override
    public IPage<ReportingVo> selectReportingPage(Page<ReportingVo> page, ReportingVo reportingVo) {
        return baseMapper.selectReportingPage(page,reportingVo);
    }

    @Transactional
    @Override
    public boolean reporting(String taskId,Integer reportCount) {
        Task task = taskMapper.selectOne(new LambdaQueryWrapper<Task>().eq(Task::getId, taskId));
        Frame frame = frameMapper.selectOne(new LambdaQueryWrapper<Frame>().eq(Frame::getId, task.getFramId()));
        //获取登录用户信息
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        Task updateTask = new Task();
        updateTask.setId(taskId);
        if (task.getReportCount()+reportCount > frame.getCount()){
            return false;
        }
        updateTask.setReportCount(task.getReportCount()+reportCount);
        updateTask.setInProcCount(task.getInProcCount()-reportCount);
        updateTask.setUpdateBy(sysUser.getRealname());
        updateTask.setUpdateTime(new Date());
        taskMapper.updateById(updateTask);
        if (frame.getStatus() != 2){
            Frame updateFrame = new Frame();
            updateFrame.setId(frame.getId());
            updateFrame.setStatus(2); //所有的报工只要改成部分报工就可以了
            frameMapper.updateById(updateFrame);
        }

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String currDay = simpleDateFormat.format(new Date());
        int count = baseMapper.selectCount(new LambdaQueryWrapper<Reporting>().ge(Reporting::getCreateTime, currDay + " 00:00:00").and(p -> p.le(Reporting::getCreateTime, currDay + " 23:59:59")));
        String prefix = "BG";
        String dateFormat = new SimpleDateFormat("yyyyMMdd").format(new Date());
        String code = prefix+dateFormat+String.format("%04d", count+1);
        Reporting reporting = new Reporting();
        reporting.setReportNo(code);
        reporting.setReportCount(reportCount);
        reporting.setQualifiedCount(0);
        reporting.setReportUserId(sysUser.getId());
        reporting.setReportTime(new Date());
        reporting.setTaskId(taskId);
        baseMapper.insert(reporting);
        return true;
    }

    @Override
    public IPage<MyReportingVo> selectMyReportingPage(Page<MyReportingVo> page, MyReportingSearchVo myReportingSearchVo) {
        return baseMapper.selectMyReportingPage(page,myReportingSearchVo);
    }

    @Override
    public Double reportTotalWeight(MyReportingSearchVo myReportingSearchVo) {
        return baseMapper.reportTotalWeight(myReportingSearchVo);
    }

    @Override
    @Transactional
    public boolean returnReport(String id) {
        Reporting reporting = baseMapper.selectOne(new LambdaQueryWrapper<Reporting>().eq(Reporting::getId, id));
        Task task = taskMapper.selectOne(new LambdaQueryWrapper<Task>().eq(Task::getId, reporting.getTaskId()));
        Task updateTask = new Task();
        updateTask.setId(reporting.getTaskId());
        updateTask.setInProcCount(task.getInProcCount()+reporting.getReportCount());
        updateTask.setReportCount(task.getReportCount()-reporting.getReportCount());
        if (task.getHandleSort() == 1 && task.getReportCount()-reporting.getReportCount()==0){
            Frame frame = new Frame();
            frame.setId(task.getFramId());
            frame.setStatus(1);
            frameMapper.updateById(frame);
        }
        taskMapper.updateById(updateTask);
        baseMapper.deleteById(id);
        return true;
    }
}


























